package com.leyou.item.controller;

import com.leyou.common.dto.BaseDTO;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    public BrandService brandService;

    @Autowired
    public CategoryBrandService categoryBrandService;

    //分页查询
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> querypage(
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "key",required = false)String key
    ){
        return ResponseEntity.ok(this.brandService.querypage(page,rows,key));
    }
    //新增品牌

    @PostMapping
    public ResponseEntity<Void> insertbrand(BrandDTO brandDTO){
        this.brandService.insertbrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //更新品牌
    @PutMapping
    public ResponseEntity<Void> updatebrand(BrandDTO brandDTO){
        this.brandService.updatebrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //根据id集合查询品牌集合
        @GetMapping("/list")
        public ResponseEntity<List<BrandDTO>> querybrandlist(@RequestParam("ids") List<Long> ids){
            return ResponseEntity.ok(this.brandService.querybrandlist(ids));
    }

    //根据分类id查询品牌
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> querycategory(@RequestParam("id") Long cid){

       // return ResponseEntity.ok(this.categoryBrandService.queryCategoryById(cid));
        return ResponseEntity.ok(brandService.queryBrandByCategory(cid));

    }

    //根据id查询品牌
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> querybrandById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.brandService.querybrandById(id));
    }

    //根据id删除品牌
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteById(@PathVariable("id")Long bid){
        this.brandService.deleteById(bid);
        return ResponseEntity.ok().build();
    }
}
